package com.lib.service;

import cn.hutool.core.util.StrUtil;
import com.lib.dao.MemberDao;
import com.lib.dao.ex.DaoException;
import com.lib.dao.ex.DeleteDaoException;
import com.lib.dao.ex.InsertDaoException;
import com.lib.dao.ex.UpdateDaoException;
import com.lib.entity.Member;
import com.lib.service.ex.*;

import java.util.List;

public class MemberService {
    private MemberDao memberDao = new MemberDao();
    //-----------添加会员
    public void insertMember(Member member) throws InValidArgumentException, EmptyArgumentException,
            InsertServiceException, ServiceException, RecordIsFoundException {
        if (StrUtil.isEmpty(member.getName())){
            throw new EmptyArgumentException("添加会员异常：会员名不能为空");
        }
        if (StrUtil.isEmpty(member.getPwd())){
            throw new EmptyArgumentException("添加会员异常：会员密码不能为空");
        }
        if (StrUtil.isEmpty(member.getPhone())){
            throw new EmptyArgumentException("添加会员异常：会员电话不能为空");
        }
        if (StrUtil.isEmpty(member.getIdNumber())){
            throw new EmptyArgumentException("添加会员异常：会员身份证不能为空");
        }
        if (member.getBalance()<=0){
            throw new InValidArgumentException("没钱就不要冲会员拉，");
        }

        Member tempmember = null;
        //判断姓名——不能重复
        try {
            tempmember = memberDao.selectMemberByName(member.getName());
        } catch (DaoException e) {
            throw new ServiceException("添加会员异常：数据库异常"+e.getMessage(),e);
        }
        if (tempmember!=null){
            throw new RecordIsFoundException("添加会员异常：会员名已存在");
        }
        //判断身份证——不能为空
        try {
            tempmember = memberDao.selectMemberByIDNumber(String.valueOf(member));
        } catch (DaoException e) {
            throw new ServiceException("添加会员异常：数据库异常"+e.getMessage(),e);
        }
        if (tempmember!=null){
            throw new RecordIsFoundException("添加会员异常：会员身份证已存在");
        }
        //添加会员
        try {
            memberDao.insertMember(member);
        } catch (DaoException e) {
            throw new ServiceException("添加会员异常：数据库异常"+e.getMessage(),e);
        } catch (InsertDaoException e) {
            throw new InsertServiceException("添加会员异常：添加失败"+e.getMessage(),e);
        }
    }
    /*    //会员充值--查找余额：
        public Member MenberRegdate(String idNumber) throws EmptyArgumentException, RecordIsFoundException, ServiceException {
            if (StrUtil.isEmpty(idNumber)){
                throw new EmptyArgumentException("充值失败：身份证不能为空");
            }
            Member tempmember = null;
            //判断身份证是否存在
            try {
                tempmember = memberDao.selectMemberByIDNumber(idNumber);
            } catch (DaoException e) {
                throw new ServiceException("添加会员异常：数据库异常"+e.getMessage(),e);
            }
            if (tempmember!=null){
                throw new RecordIsFoundException("添加会员异常：会员身份证已存在");
            }
            Member member = null;
            try {
                member = memberDao.selectMemberBalanceByidNumber(idNumber);
            } catch (DaoException e) {
                throw new ServiceException("余额查询异常：查询失败");
            }
            return member;
        }*/
    //会员充值--余额更新
    public void UpdateMemberRegdate(String idNumber,String balance) throws UpdateServiceException, EmptyArgumentException, RecordIsFoundException, ServiceException {
        if (StrUtil.isEmpty(idNumber)){
            throw new EmptyArgumentException("更新失败：身份证不能为空");
        }
        if (StrUtil.isEmpty(balance)){
            throw new EmptyArgumentException("更新失败：金额为空。");
        }
        Member tempmember = null;
        //判断身份证是否存在
        try {
            tempmember = memberDao.selectMemberByIDNumber(idNumber);
        } catch (DaoException e) {
            throw new ServiceException("添加会员异常：数据库异常"+e.getMessage(),e);
        }
        if (tempmember == null){
            throw new RecordIsFoundException("添加会员异常：会员身份证不存在");
        }
        String newBalance = Float.toString(tempmember.getBalance()+Float.valueOf(balance));
        try {
            memberDao.updateMemberBalance(idNumber,newBalance);
        } catch (UpdateDaoException e) {
            throw new UpdateServiceException("余额更新异常：更新失败");
        } catch (DaoException e) {
            throw new ServiceException("余额更新异常：数据库异常");
        }
    }
    //会员修改
    public void modifyMember(Member member) throws EmptyArgumentException, InValidArgumentException, RecordIsFoundException, UpdateDaoException, ServiceException {
        if (StrUtil.isEmpty(member.getName())){
            throw new EmptyArgumentException("添加会员异常：会员名不能为空");
        }
        if (StrUtil.isEmpty(member.getPhone())){
            throw new EmptyArgumentException("添加会员异常：会员电话不能为空");
        }
        if (member.getBalance()<=0){
            throw new InValidArgumentException("没钱就不要冲会员拉，");
        }

        Member tempmember = null;
        //判断姓名——不能重复
        try {
            tempmember = memberDao.selectMemberByName(member.getName());
        } catch (DaoException e) {
            throw new ServiceException("添加会员异常：数据库异常"+e.getMessage(),e);
        }
        if (tempmember!=null){
            throw new RecordIsFoundException("添加会员异常：会员名已存在");
        }
        try {
            memberDao.updateMember(member);
        } catch (ServiceException e) {
            throw new ServiceException("更新会员异常：数据库异常"+e.getMessage(),e);
        } catch (UpdateDaoException e) {
            throw new UpdateDaoException("更新会员异常：更新失败"+e.getMessage(),e);
        }
        //判断身份证——不能为空
      /*  try {
            tempmember = memberDao.selectMemberByIDNumber(String.valueOf(member));
        } catch (DaoException e) {
            throw new ServiceException("添加会员异常：数据库异常"+e.getMessage(),e);
        }
        if (tempmember!=null){
            throw new RecordIsFoundException("添加会员异常：会员身份证已存在");
        }*/
    }
    //会员删除
    public void deleteMember(String idNumber) throws DeleteServiceException, ServiceException {
        System.out.println("service-idnumber="+idNumber);
        try {
            memberDao.deleteMember(idNumber);
        } catch (ServiceException e) {
            throw new ServiceException("删除会员异常：数据库异常"+e.getMessage(),e);
        } catch (DeleteDaoException e) {
            throw new DeleteServiceException("删除会员异常：删除失败"+e.getMessage(),e);
        }
    }
    //查找会员信息
    public List<Member> GetMemberList() throws EmptyArgumentException, ServiceException {
        List<Member> memberList = null;
        /*if (StrUtil.isEmpty(idNumber)){
            throw new EmptyArgumentException("查找会员信息失败：idNumber为空");
        }*/
        try {
            memberList = memberDao.GetMemberList();
        } catch (DaoException e) {
            throw new ServiceException("查找会员信息失败;数据库异常"+e.getMessage(),e);
        }
        return memberList;
    }
}
